<cfcomponent displayname="StructDriver" hint="This class is a dictionary driver to manager Struct Object" output="false">
	<cfscript>
		/**
		* 	Domain cast some struct data to array Method
		* 	@name asArray
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-23
		*	@return Array
		**/
		public Array function asArray( Struct data ) {
			var arrDataSet 	 = [];
			var structSet  	 = arguments.data;
			var structFields = StructKeyList( structSet );
			
			for( var idx = 1; idx <= ListLen( structFields ); idx++ ) {
				ArrayAppend( arrDataSet , structSet[ ListGetAt( structFields , idx ) ] );
			}
			
			return arrDataSet;
		}
		
		/**
		* 	Domain cast some struct data to query Method
		* 	@name asQuery
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-25
		*	@return Query
		**/
		public Query function asQuery( Struct data ) {
			var structData  = arguments.data;
			var fieldList 	= LCase( StructKeyList( structData ) );
			var rsDataSet  	= QueryNew(fieldList);
			var key			= "";
			
			rsDataSet = QueryNew( fieldList );
			
			for( row in structData ) {
				QueryAddRow( rsDataSet );
				for( var idx = 1; idx <= ListLen( fieldList ); idx++ ){
					key = ListGetAt( fieldList , idx );
					QuerySetCell( rsDataSet , key , row[ key ] );
				}
			}
						
			return rsDataSet;
		}
		
		/**
		* 	get an item of object Method
		* 	@name getItem
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-28
		*	@return Struct
		**/
		public Struct function getItem( Struct data , String field ,Any item ) {
			var structDataSet	= [];
			var item			= arguments.item;
			var data			= arguments.data;
			var field			= arguments.field;
			
			if( Len( field ) > 0 ) {
				if( StructKeyExists( data , field ) == true ) {
					if( CompareNoCase( data[ field ] , item ) == 0 ) {
						structDataSet[ field ] = data[ field ];
					}
				}
				else {
					variables.error = Application.instance.factory.getModels("Error");
					variables.error.handlerError( Application.config.customExceptionUser , "The object data not contains " & field & " key" );
				}
			}
			else {
				variables.error = Application.instance.factory.getModels("Error");
				variables.error.handlerError( Application.config.customExceptionUser , "Is necessaire to pass a param 'key' to the struct" );
			}
					
			return structDataSet;
		}
		
		/**
		* 	set data in the object Method
		* 	@name add
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-29
		*	@return Struct
		**/
		public Struct function add( Struct source , Struct data ) {
			var structDataSet	= arguments.source;
			var data			= arguments.data;
			
			StructAppend( structDataSet , data );
						
			return structDataSet;
		}
		
		/**
		* 	Domain determin sort field Method
		* 	@name asc
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-29
		*	@return String
		**/
		public String function asc( String field ) {
			var order = arguments.recordSet & ",ASC";
			
			return order;
		}
		
		/**
		* 	Domain determin sort field Method
		* 	@name desc
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-29
		*	@return String
		**/
		public String function desc( String field ) {
			var order = arguments.recordSet & ",DESC";
			
			return order;
		}
		
		/**
		* 	Domain sort data Method
		* 	@name sort
		* 	@access public
		* 	@author Paulo Teixeira
		*	@date 2012-05-29
		*	@return Struct
		**/
		public Struct function sort( Struct recordSet , String filter ) {
			var structRecordSet = {};
			var recordSet 		= arguments.recordSet;
			var filter 			= { order : ListGetAt( arguments.filter , 1 ) , field : ListGetAt( arguments.filter , 2 ) };
			
			structRecordSet = StructSort( recordSet , "text" , filter.order , filter.field );
			
			return structRecordSet;
		}
	</cfscript>
</cfcomponent>